package com.slz3.business.module.orderManagement.ordOrderLogistics.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrder;
import com.slz3.business.module.orderManagement.ordOrderLogistics.domain.OrdOrderLogistics;
import com.slz3.business.module.orderManagement.ordOrderLogistics.domain.OrdOrderLogisticsFactory;
import com.slz3.business.module.orderManagement.ordOrderLogistics.steward.OrdOrderLogisticsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class OrdOrderLogisticsServiceImpl implements OrdOrderLogisticsService {
    @Override
    public OrdOrderLogistics save(OrdOrderLogistics ordOrderLogistics) {
        OrdOrderLogistics dbOrdOrderLogistics = Repositorys.ordOrderLogistics.save(OrdOrderLogisticsFactory.initBaseFields(ordOrderLogistics));

        //修改订单状态
        OrdOrder ordOrder = Services.ordOrder.single(ordOrderLogistics.getOrderUuid()).orElse(null);
        if(VE.isNotEmpty(ordOrder)){
            ordOrder.setSerDeliveryTime(LocalDateTime.now());
            ordOrder.setSerOrderStatus(3);
            Services.ordOrder.modify(ordOrder);
            //将物流信息存入redis
            Jedis.db0.set(RK.SERVICE__ORDER_LOGISTICS__ORDER_UUID__$UUID$.replace("${uuid}",ordOrder.getUuid().toString()), Steward.obj2Map(ordOrderLogistics));

            //存储物流单号到订单的映射
            Jedis.db0.set(RK.OrdOrder.getOrderUuidByLogisticsNo(dbOrdOrderLogistics.getLogisticsNo()),ordOrder.getUuid().toString());

            //同步修改redis的订单状态
            OrdOrderLogisticsFactory.syrcRedisOrder(ordOrder);
        }
        return dbOrdOrderLogistics;
    }
    @Override
    public Page<OrdOrderLogistics> page(MultiValueMap<String, String> params) {
        Page<OrdOrderLogistics> ordOrderLogisticsPage = Repositorys.ordOrderLogistics.findAll(
                OrdOrderLogisticsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return ordOrderLogisticsPage;
    }


    @Override
    public OrdOrderLogistics modify(OrdOrderLogistics ordOrderLogisticsNew) {
        OrdOrderLogistics dbOrdOrderLogistics = Repositorys.ordOrderLogistics.saveAndFlush(OrdOrderLogisticsFactory.initBaseFields(ordOrderLogisticsNew));
        //将物流信息存入redis
        Jedis.db0.set(RK.SERVICE__ORDER_LOGISTICS__ORDER_UUID__$UUID$.replace("${uuid}",dbOrdOrderLogistics.getOrderUuid().toString()), Steward.obj2Map(dbOrdOrderLogistics));
        //存储物流单号到订单的映射
        Jedis.db0.set(RK.OrdOrder.getOrderUuidByLogisticsNo(dbOrdOrderLogistics.getLogisticsNo()),dbOrdOrderLogistics.getOrderUuid().toString());
        return dbOrdOrderLogistics;
    }

    @Override
    public Optional<OrdOrderLogistics> single(UUID uuid) {
        return Repositorys.ordOrderLogistics.findById(uuid);
    }

    @Override
    public Optional<OrdOrderLogistics> query(MultiValueMap<String, String> params) {
        return Repositorys.ordOrderLogistics.findOne(OrdOrderLogisticsSpecification.findJoin(params));
    }

    @Override
    public List<OrdOrderLogistics> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.ordOrderLogistics.findAllById(uuids);
    }

    @Override
    public List<OrdOrderLogistics> delete(List<OrdOrderLogistics> ordOrderLogisticsList) {
        ordOrderLogisticsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            OrdOrderLogisticsFactory.initBaseFields(item);
        });
        return Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
    }

}
